//
//  BuildingTableViewCell.m
//  OutesIOS
//
//  Created by norman on 2019/1/12.
//  Copyright © 2019 norman. All rights reserved.
//

#import "BuildingTableViewCell.h"
#import "UITapImageView.h"

#define CELL_PADDING_LR 15
#define CELL_PADDING_TB 5

@interface BuildingTableViewCell()

@property (nonatomic,strong)UIView *bgView;

@property (nonatomic,strong)UILabel *lbName;

@property (nonatomic,strong)UILabel *lbArea;

@property (nonatomic,strong)UILabel *lbLoad;

@property (nonatomic,strong)UILabel *lbEquip;

@property (nonatomic,strong)UITapImageView *chk;

@end

@implementation BuildingTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.contentView.backgroundColor = UIColor16(0xeeeeee);
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(CELL_PADDING_TB, CELL_PADDING_TB, kScreen_Width - 2*CELL_PADDING_TB, 0)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 4;
    //阴影效果
    //    self.bgView.layer.shadowColor = UIColor16(0x000000).CGColor;
    //    self.bgView.layer.shadowOffset = CGSizeMake(2, 4);
    //    self.bgView.layer.shadowOpacity = 0.5;
    //    self.bgView.layer.shadowRadius = 4;
    [self.contentView addSubview:self.bgView];
    
    //编辑图标
    UIImageView *edit = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"edit"]];
    edit.x = self.bgView.width - edit.width - CELL_PADDING_LR;
//    [self.bgView addSubview:edit];
    
    //名称
    self.lbName = [UILabel labelSingleLineFontSize:kListTitleFontSize content:@"客厅"];
    self.lbName.textColor = BSBlackTextColor;
    self.lbName.x = CELL_PADDING_LR;
    self.lbName.y = CELL_PADDING_LR;
    self.lbName.width = kScreen_Width - 2*CELL_PADDING_LR;
    edit.centerY = self.lbName.centerY;
    [self.bgView addSubview:self.lbName];
    
    UILabel *lb1 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"面积："];
    lb1.textColor = CM_TABLE_DETAIL_COLOR;
    lb1.x = CELL_PADDING_LR;
    lb1.y = CGRectGetMaxY(self.lbName.frame) + CELL_PADDING_LR;
    [self.bgView addSubview:lb1];
    
    self.lbArea = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"张国其"];
    self.lbArea.width += 20;
    self.lbArea.textColor = CM_TABLE_TITLE_COLOR;
    self.lbArea.x = CGRectGetMaxX(lb1.frame) + 2;
    self.lbArea.y = CGRectGetMaxY(self.lbName.frame) + CELL_PADDING_LR;
    [self.bgView addSubview:self.lbArea];
    
    UILabel *lb4 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"负荷："];
    lb4.textColor = CM_TABLE_DETAIL_COLOR;
    lb4.x = self.bgView.width/2;
    lb4.y = self.lbArea.y;
    [self.bgView addSubview:lb4];
    
    self.lbLoad = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lb4.frame) + 2, self.lbArea.y, 0, 0)];
    self.lbLoad.textColor = CM_TABLE_TITLE_COLOR;
    self.lbLoad.font = [UIFont systemFontOfSize:kActionBarFontSize];
    self.lbLoad.size = [Tools sizeOfSingleLineText:@"13671036827" withSize:kActionBarFontSize];
    self.lbLoad.width += 20;
    self.lbLoad.text = @"13671036827";
    [self.bgView addSubview:self.lbLoad];
    
    
    UILabel *lb2 = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"设备："];
    lb2.textColor = CM_TABLE_DETAIL_COLOR;
    lb2.tag = 100;
    lb2.x = CELL_PADDING_LR;
    lb2.y = CGRectGetMaxY(self.lbLoad.frame) + CELL_PADDING_LR;
    [self.bgView addSubview:lb2];
    
    self.lbEquip = [UILabel labelSingleLineFontSize:kActionBarFontSize content:@"待定"];
    self.lbEquip.width = self.bgView.width - 2*CELL_PADDING_LR;
    self.lbEquip.textColor = CM_TABLE_TITLE_COLOR;
    self.lbEquip.x = CGRectGetMaxX(lb2.frame) + 2;
    self.lbEquip.y = CGRectGetMaxY(self.lbLoad.frame) + CELL_PADDING_LR;
    [self.bgView addSubview:self.lbEquip];
    
    self.bgView.height = CGRectGetMaxY(self.lbEquip.frame) + CELL_PADDING_LR;
    
    UIImage *img = [UIImage imageNamed:@"ic_unchecked"];
    self.chk = [[UITapImageView alloc]initWithImage:img];
    self.chk.x = self.bgView.width - self.chk.width - CELL_PADDING_LR;
    self.chk.centerY = self.bgView.height/2;
    [self.bgView addSubview:self.chk];
    self.chk.tag = 0;
    @weakObj(self)
    [self.chk addTargetBlock:^(id obj) {
        if (Weakself.chk.tag == 0) {
            Weakself.chk.image = [UIImage imageNamed:@"ic_checked"];
            Weakself.chk.tag = 1;
        } else {
            Weakself.chk.image = [UIImage imageNamed:@"ic_unchecked"];
            Weakself.chk.tag = 0;
        }
    }];
}

- (BOOL)cellSelected {
    if (self.chk.hidden == NO && self.chk.tag == 1) {
        return YES;
    }
    return NO;
}

- (void)setData:(BuildingData*)data {
    self.lbName.text = data.building_name;
    self.lbLoad.text = [NSString stringWithFormat:@"%0.1f kW", data.total_load];
    self.lbArea.text = [NSString stringWithFormat:@"%.0f ㎡", data.width*data.length*data.floor_count];
    if ([Tools isEmpty:data.equip]) {
        self.lbEquip.text = @"待定";
    } else {
        self.lbEquip.attributedText = [self getDeviceText:data.equip andNumber:data.quantity];
    }
    if (data.total_load > 0) {
        self.chk.hidden = NO;
    } else {
        self.chk.hidden = YES;
    }
}

- (NSMutableAttributedString*)getDeviceText:(NSString*)device andNumber:(long)number {
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  X  %ld台", device, number]];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:kActionBarFontSize]
                          range:NSMakeRange(0, AttributedStr.length)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:CM_TABLE_TITLE_COLOR
                          range:NSMakeRange(0, AttributedStr.length)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:CM_ORANGE_COLOR
                          range:NSMakeRange([device length], 4)];
    return AttributedStr;
}

+ (CGFloat)cellHeight {
    CGFloat h = 2*CELL_PADDING_TB;
    h += 3*(CELL_PADDING_LR + [Tools sizeOfSingleLineText:@"高度" withSize:kActionBarFontSize].height) + CELL_PADDING_LR;
    return h;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
